package com.fcat.common.user.service.impl;

import com.fcat.common.user.vo.PermissionVO;
import org.springframework.stereotype.Service;
import com.fcat.springboot.mybatis.service.impl.TsuantoneBaseServiceImpl;
import com.fcat.common.user.mapper.PermissionMapper;
import com.fcat.common.user.entity.Permission;

import com.fcat.common.user.service.PermissionService;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl extends TsuantoneBaseServiceImpl<PermissionMapper,Permission> implements PermissionService{

    @Override
    public List<Permission> getListByRoleIds(List<Long> longs) {
        return mapper.getListByRoleIds(longs);
    }

    @Override
    public List<PermissionVO> getAll() {
        return mapper.getAll();
    }

    @Override
    public List<Permission> getPermissionByRoleId(Long roleId) {
        Map<String,Object> map=new HashMap<>();
        map.put("roleId",roleId);
        return mapper.getPermissionByRoleId(map);
    }

    @Override
    public List<PermissionVO> getTree(List<PermissionVO> list,Long parentId) {
        List<PermissionVO> permissionVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (PermissionVO permissionVO : list) {
                if(permissionVO.getParentId()==parentId){
                    PermissionVO itemPermission=new PermissionVO();
                    itemPermission.setId(permissionVO.getId());
                    itemPermission.setName(permissionVO.getName());
                    itemPermission.setParentId(permissionVO.getParentId());
                    itemPermission.setFlag(permissionVO.getFlag());
                    itemPermission.setChildren(getTree(list, permissionVO.getId()));
                    permissionVOList.add(itemPermission);
                }
            }
        }
        return permissionVOList;
    }

    @Override
    public List<Permission> getListByUserId(Long id) {
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        return mapper.getListByUserId(map);
    }
}